package de.awg.accountanalyzer.entities;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;

/*
 * Java Bean in corresponding to JavaFx Java Bean
 * This is needed for JavaFX binding features
 * 
 */

public class AccountPosition extends ChartEntity {

	private SimpleStringProperty auftragskonto = new SimpleStringProperty();
	private SimpleStringProperty buchungstag = new SimpleStringProperty();
	private SimpleObjectProperty<Date> valutadatum = new SimpleObjectProperty<Date>();
	private SimpleStringProperty buchungstext = new SimpleStringProperty();
	private SimpleStringProperty verwendungszweck = new SimpleStringProperty();
	private SimpleStringProperty kreditor_Debitor = new SimpleStringProperty();
	private SimpleStringProperty kontonummer = new SimpleStringProperty();
	private SimpleStringProperty blz = new SimpleStringProperty();
	private SimpleDoubleProperty betrag = new SimpleDoubleProperty();
	private SimpleStringProperty waehrung = new SimpleStringProperty();
	private SimpleStringProperty info = new SimpleStringProperty();
	private SimpleBooleanProperty visible = new SimpleBooleanProperty();
	private volatile int hashCode;

	static NumberFormat numberFormat = null;
	static DateFormat dateFormat = null;
	static {

		numberFormat = NumberFormat.getInstance(Locale.GERMAN);
		dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);

	}

	public String getAccountName() {
		return this.getBLZ() + " " + this.getKontonummer();
	}

	public String getAuftragskonto() {
		return auftragskonto.get();
	}

	public void setAuftragskonto(String auftragskonto) {
		this.auftragskonto.set(auftragskonto);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty auftragskontoProperty() {
		return auftragskonto;
	}

	public String getBuchungstag() {
		return buchungstag.get();
	}

	public void setBuchungstag(String buchungstag) {
		this.buchungstag.set(buchungstag);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty buchungstagProperty() {
		return buchungstag;
	}

	public Date getValutadatum() {
		return valutadatum.get();
	}

	public void setValutadatum(Date valutadatum) {
		this.valutadatum.set(valutadatum);
	}

	// hold javafx beans contract by providing this method
	public SimpleObjectProperty<Date> valutadatumProperty() {
		return valutadatum;
	}

	public String getBuchungstext() {
		return buchungstext.get();
	}

	public void setBuchungstext(String buchungstext) {
		this.buchungstext.set(buchungstext);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty buchungstextProperty() {
		return buchungstext;
	}

	public String getVerwendungszweck() {
		return verwendungszweck.get();
	}

	public void setVerwendungszweck(String verwendungszweck) {
		this.verwendungszweck.set(verwendungszweck);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty verwendungszweckProperty() {
		return this.verwendungszweck;
	}

	public String getKreditor_Debitor() {
		return kreditor_Debitor.get();
	}

	public void setKreditor_Debitor(String kreditor_Debitor) {
		this.kreditor_Debitor.set(kreditor_Debitor);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty kreditor_DebitorProperty() {
		return kreditor_Debitor;
	}

	public String getKontonummer() {
		return kontonummer.get();
	}

	public void setKontonummer(String kontonummer) {
		this.kontonummer.set(kontonummer);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty kontonummerProperty() {
		return kontonummer;
	}

	public String getBLZ() {
		return blz.get();
	}

	public void setBLZ(String bLZ) {
		this.blz.set(bLZ);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty blzProperty() {
		return blz;
	}

	public double getBetrag() {
		return betrag.get();
	}

	public void setBetrag(double betrag) {
		this.betrag.set(betrag);
	}

	// hold javafx beans contract by providing this method
	public SimpleDoubleProperty betragProperty() {
		return betrag;
	}

	public String getBetragFormated() {
		Currency cur = Currency.getInstance(this.getWaehrung());
		numberFormat.setCurrency(cur);
		return numberFormat.format(getBetrag());
	}

	public String getValutadatumFormated() {
		return dateFormat.format(valutadatum.get());
	}

	public String getWaehrung() {
		return waehrung.get();
	}

	public void setWaehrung(String waehrung) {
		this.waehrung.set(waehrung);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty waehrungProperty() {
		return waehrung;
	}

	public boolean getVisible() {
		return visible.get();
	}

	public void setVisible(boolean visible) {
		this.visible.set(visible);
	}

	// hold javafx beans contract by providing this method
	public SimpleBooleanProperty visibleProperty() {
		return visible;
	}

	public String getInfo() {
		return info.get();
	}

	public void setInfo(String info) {
		this.info.set(info);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty infoProperty() {
		return info;
	}

	@Override
	public boolean equals(Object other) {

		if (other == this) {
			return true;
		}
		if (!(other instanceof AccountPosition)) {
			return false;
		}
		return this.hashCode() == other.hashCode();
	}

	@Override
	public int hashCode() {
		int lResult = hashCode;
		if (lResult == 0) {
			lResult = 17;
			lResult = 31 * lResult + getValutadatum().hashCode();
			lResult = 31 * lResult + getKreditor_Debitor().hashCode();
			lResult = 31 * lResult + getKontonummer().hashCode();
			lResult = 31 * lResult + new Double(getBetrag()).hashCode();
		}
		hashCode = lResult;
		return lResult;
	}

	@Override
	public String toString() {
		return "AccountPosition [Auftragskonto=" + getAuftragskonto() + ", Buchungstag=" + getBuchungstag() + ", Valutadatum=" + getValutadatumFormated() + ", Buchungstext=" + getBuchungstext()
				+ ", Verwendungszweck=" + getVerwendungszweck() + ", Kreditor_Debitor=" + getKreditor_Debitor() + ", Kontonummer=" + getKontonummer() + ", BLZ=" + getBLZ() + ", Betrag="
				+ getBetragFormated() + ", Waehrung=" + getWaehrung() + ", Info=" + getInfo() + "]";
	}

	@Override
	public double getValue() {
		return this.getBetrag();
	}

	@Override
	public String getName() {
		return this.kreditor_Debitor + " " + this.getBLZ() + " " + this.getKontonummer();
	}
}
